/**
 @Name：角色管理
 @Author：cike
 @Date：2019-5-26
**/

layui.define(['table', 'form','treeGrid'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,setter = layui.setter
  ,form = layui.form
  ,treeGrid = layui.treeGrid;

  treeGrid.set({
    where: {
      access_token: layui.data(setter.tableName)[setter.request.tokenName]
    }
    ,method: 'post'
    ,response:{
      statusCode: 200
    }
    ,page: true
    ,text: { none : "暂无相关数据" }
    ,done: function (res) {
      if(res[setter.response.statusCode] == setter.response.statusCode.logout){
        view.exit();
      }
    }
    ,parseData: function(res){ //res 即为原始返回的数据
      return {
        "code": res.code, //解析接口状态
        "msg": res.msg, //解析提示文本
        "count": res.data.total, //解析数据长度
        "data": res.data.list //解析数据列表
      };
    }
  });



  treeGrid.render({
    id:'back-permission-list'
    ,elem: '#back-permission-list'
    ,url:setter.api_back_permission_list
    ,cellMinWidth: 100
    ,idField:'id'//必須字段
    ,treeId:'id'//树形id字段名称
    ,treeUpId:'pid'//树形父id字段名称
    ,treeShowName:'name'//以树形式显示的字段
    // ,height:600
    ,isFilter:false
    ,iconOpen:false
    ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
    ,loading:true
    ,isPage:false
    ,limit:999
    ,cols: [[
        {field:'id', width: 65, title: '编号'}
        ,{field:'pid', width: 65, title: '上级'}
        ,{field:'sort', width: 65, title: '排序', edit:'text'}
        ,{field:'name', title: '名称' ,width: 150}
        ,{field:'path', title: '路径' ,width: 200}
        ,{field:'desc', title: '描述'}
        ,{field:'status', width: 100, title:'状态', templet: '#buttonSwitch', minWidth: 80, align: 'center'}
        ,{field:'update_time', title: '更新时间'}
        ,{field:'create_time', title: '创建时间'}
        ,{title:'操作', width: 150, align: 'center', toolbar: '#back-permission-list-operation'}
    ]]
  });
  //监听工具条
    
  treeGrid.on('edit(back-permission-list)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
    admin.req({
      url: setter.api_back_permission_update
      ,data: obj.data
      ,type: 'post'
      ,done: function(res){
        layer.msg(res.msg);
        treeGrid.reload('back-permission-list'); //重载表格
      }
    });
  });
  treeGrid.on('tool(back-permission-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此路由？', function(index){
        admin.req({
          url: setter.api_back_permission_delete
          ,data: {id:obj.data.id}
          ,type: 'post'
          ,done: function(res){
            layer.msg(res.msg);
            treeGrid.reload('back-permission-list'); //重载表格
          }
        });
        layer.close(index); //执行关闭 
      });
    }else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑路由'
        ,area: admin.screen() < 2 ? ['100%','530px'] : ['450px','530px']
        ,id: 'back-permission-edit-popup'
        ,success: function(layero, index){
          view(this.id).render('role/permissionform', data).done(function(){
            // 上级选择
            admin.req({
              url: setter.api_back_permission_list
              ,done: function(res){
                var list = res.data.list;
                list.unshift({"id":0,"pid":0,"name":"空","status":1})
                $.each(list, function (index, item) {
                  if(item.pid == 0 && item.status == 1 && item.id != data.id){
                    is_checked = item.id == data.pid?'checked':'';
                    $('#back-permission-form-permission-select').append(new Option(item.name, item.id, is_checked, is_checked));// 下拉菜单里添加元素
                  }
                });
                form.render(null, 'back-permission-form');
              }
            });
            //监听提交
            form.on('submit(back-permission-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              admin.req({
                url: setter.api_back_permission_update
                ,data: field
                ,type: 'post'
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭
                  treeGrid.reload('back-permission-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }
  });

  exports('permission', {})
});